import noticeService, { queryNotice } from "../service/notice";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from 'element-plus'

interface noticeAction {
    query: (obj: queryNotice, callback: any) => void,
    select: (callback: any) => void,
    insert: (obj: queryNotice, queryData: queryNotice, callback: any) => void,
    deleted: (id: number, queryData: queryNotice, callback: any) => void,
    update: (obj: queryNotice, queryData: queryNotice, callback: any) => void,
    getById: (id: number | string, callback: any) => void,
}


export function noticeHook(): noticeAction {
    const router = useRouter()

    //查询函数
    function query(obj: queryNotice, callback: any) {
        noticeService.query(obj).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                console.log("请求信息", res.data)
                callback(res.data.data, res.data.count)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 查询全部，无分页
    function select(callback: any) {
        noticeService.select().then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                console.log("请求信息", res.data)
                callback(res.data.data, res.data.count)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 添加
    function insert(obj: queryNotice, queryData: queryNotice, callback: any) {
        noticeService.insert(obj).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })
                query(queryData, callback)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 删除
    function deleted(id: number, queryData: queryNotice, callback: any) {
        noticeService.deleted(id).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })
                query(queryData, callback)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 修改
    function update(obj: queryNotice, queryData: queryNotice, callback: any) {
        noticeService.update(obj).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })
                query(queryData, callback)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // id获取回显
    function getById(id: number | string, callback: any) {
        noticeService.getById(id).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                // ElMessage({ type: "success", message: res.data.msg })
                console.log("修改回显数据", res.data.data)
                callback(res.data.data)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    return {
        query,
        select,
        insert,
        deleted,
        update,
        getById
    }
}



